#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();
        unordered_map<int, int> index;
        for (int i = 0; i < n; i++) index[arr[i]] = i;
        int ret = 0;
        int dp[1001][1001] = { 0 };
        for (int i = 1; i < n; i++)
        {
            for (int j = i - 1; j >= 0; j--)
            {
                int tgt = arr[i] - arr[j];
                int idx = index.count(tgt) ? index[tgt] : -1;
                if (idx == -1 || idx >= j) dp[i][j] = 2;
                else dp[i][j] = dp[j][idx] + 1;
                ret = max(ret, dp[i][j]);
            }
        }
        return ret == 2 ? 0 : ret;
    }
};